package cn.lhl.web.controller.system;

import cn.lhl.domain.system.Module;
import cn.lhl.service.system.ModuleService;
import cn.lhl.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseController {
    @Autowired
    private ModuleService moduleService;

    @RequiresPermissions("模块管理")
    @RequestMapping(value = "/list", name = "查询所有分页的结果")
    public String findAll(@RequestParam(defaultValue = "1") int page,
                          @RequestParam(defaultValue = "5") int size) {
        PageInfo pageInfo = moduleService.findAll(page, size);
        model.addAttribute("page", pageInfo);
        return "/system/module/module-list";
    }

    @RequestMapping(value = "/toAdd", name = "去增加模块界面")
    public String toAdd() {
        List<Module> moduleList = moduleService.findAll();
        model.addAttribute("menus", moduleList);
        return "/system/module/module-add";
    }

    @RequestMapping(value = "/toUpdate", name = "去修改模块界面")
    public String toUpdate(String id) {
        List<Module> moduleList = moduleService.findAll();
        model.addAttribute("menus", moduleList);
        //根据id查询出模块数据
        Module module = moduleService.findById(id);
        model.addAttribute("module", module);
        return "/system/module/module-update";
    }

    @RequestMapping(value = "/edit", name = "修改或新增模块")
    public String update(Module module) {
        //判断是否有module_id
        if (StringUtil.isEmpty(module.getId())) {
            module.setId(UUID.randomUUID().toString());
            moduleService.save(module);
        } else {
            moduleService.update(module);
        }
        return "redirect:/system/module/list.do";
    }

    @RequestMapping(value = "/delete", name = "删除模块")
    public String delete(String id) {
        moduleService.delete(id);
        return "redirect:/system/module/list.do";
    }


}
